/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Test;

import java.awt.Font;
import java.awt.Rectangle;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.UIManager;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

/**
 *
 * @author myguel
 */
public class MinimumTest
{
    public MinimumTest()
    {
        // get the default width for iconified JInternalFrames
        int desktopIconWidth = UIManager.getInt("DesktopIcon.width");
        System.out.println("desktopIconWidth = " + desktopIconWidth);
 
        // compute new value for width of iconified JInternalFrames
        // using these strings for titles
        String[] titles = { "a longer name frame test test test!!!", "another name" };
 
        // width of JInternalFrame with no title
        int minFrameWidth = computeMinFrameWidth();
        System.out.println("minFrameWidth = " + minFrameWidth);
 
        // find new width for JInternalFrame with longest title
        desktopIconWidth = computeDesktopIconWidth(minFrameWidth, titles);
        System.out.println("desktopIconWidth = " + desktopIconWidth);
 
        // update defaults table with new width via UIManager
        UIManager.put("DesktopIcon.width", desktopIconWidth);
 
        JDesktopPane desktop = new JDesktopPane();
        for(int j = 0; j < 2; j++)
        {
            JInternalFrame iframe = getIFrame(titles[j], (j + 1));
            iframe.addInternalFrameListener(ifl);
            desktop.add(iframe);
        }
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(desktop);
        f.setSize(500,500);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
 
    private int computeMinFrameWidth()
    {
        JInternalFrame f = new JInternalFrame("", true, true, true, true);
        f.pack();
        Rectangle r = f.getBounds();
        f.dispose();
        return r.width;
    }
 
    private int computeDesktopIconWidth(int minWidth, String[] titles)
    {
        FontRenderContext frc = new FontRenderContext(null,false, false);
        Font font = UIManager.getFont("InternalFrame.titleFont");
                    // default font = ("dialog", Font.BOLD, 12)
        int maxTitleWidth = 0;
        for(int j = 0; j < titles.length; j++)
        {
            Rectangle2D r = new TextLayout(titles[j], font, frc).getBounds();
            int textWidth = (int)r.getWidth();
            if(textWidth > maxTitleWidth)
                maxTitleWidth = textWidth;
        }
        int totalWidth = minWidth + maxTitleWidth;
        return totalWidth;
    }
 
    private JInternalFrame getIFrame(String title, int n)
    {
        JInternalFrame f = new JInternalFrame(title, true, true, true, true);
        f.setSize(300,300);
        int x = n * 75;
        int y = n * 75;
        f.setLocation(x, y);
        f.setVisible(true);
        return f;
    }
 
    InternalFrameAdapter ifl = new InternalFrameAdapter()
    {
        public void internalFrameIconified(InternalFrameEvent e)
        {
            JInternalFrame iframe = e.getInternalFrame();
            JInternalFrame.JDesktopIcon icon = iframe.getDesktopIcon();
            Rectangle r = icon.getBounds();
            System.out.println("icon size = " + r.width + ", " + r.height);
        }
    };
 
    public static void main(String[] args)
    {
        new MinimumTest();
    }
}

